﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Hfabase.Configs
{
    /// <summary>
    /// 管理配置类
    /// </summary>
    public class ConfigManager : Collections.Singleton<ConfigManager>, IConfig
    {
        /// <summary>
        /// 默认配置文件路径
        /// 可以修改
        /// </summary>
        public static string DefaultConfigFile =Util.EnvironmentHelper.GetEnvorimentHelper()+ @"\config\appconfig.xml";

        /// <summary>
        /// 内部使用的实例
        /// </summary>
        private IConfig configInstance = null;

        #region IConfig 成员

        public bool Load(string configFilePath = null)
        {
            //检测当前使用的类型，暂时只使用 xml
            this.configInstance = new XmlConfig();
            return this.configInstance.Load(configFilePath);
        }

        public bool Save(string newConfigFilePath = null)
        {
            if (this.configInstance == null)
            {
                throw new InvalidOperationException();
            }
            return this.configInstance.Save(newConfigFilePath);
        }

        public bool Restore(string configFilePath = null)
        {
            if (this.configInstance == null)
            {
                throw new InvalidOperationException();
            }
            return this.configInstance.Restore(configFilePath);
        }

        public string GetNodeValue(string path)
        {
            if (this.configInstance == null)
            {
                throw new InvalidOperationException();
            }

            return this.configInstance.GetNodeValue(path);
        }

        public string GetNodeAttrValue(string path, string attrName)
        {
            if (this.configInstance == null)
            {
                throw new InvalidOperationException();
            }
            return this.configInstance.GetNodeAttrValue(path, attrName);
        }

        public bool SetNodeValue(string path, string value)
        {
            if (this.configInstance == null)
            {
                throw new InvalidOperationException();
            }
            return this.configInstance.SetNodeValue(path, value);
        }

        public bool SetNodeAttrValue(string path, string attrName, string value)
        {
            if (this.configInstance == null)
            {
                throw new InvalidOperationException();
            }
            return this.configInstance.SetNodeAttrValue(path, attrName, value);
        }

        #endregion
    }
}
